//
//  XMUserHomePageViewController.m
//  XMJJ_zml
//
//  Created by 丁浩 on 2025/7/1.
//

#import "XMUserHomePageViewController.h"
#import "XMUserHomePageView.h"
#import "XMNetwork+home.h"
#import "XMNetwork+mine.h"
#import "XMMyMeetingViewController.h"
#import "XMPicwordModel.h"

@interface XMUserHomePageViewController ()<XMUserHomePageViewDelegate>

@property (nonatomic, strong) XMUserHomePageView *homePageView;

@property (nonatomic, copy) NSString *attentionStatus;

@end

@implementation XMUserHomePageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatUI];
}

- (void)creatUI {
    [self.view addSubview:self.homePageView];
    dispatch_group_t downloadGroup = dispatch_group_create();
    dispatch_group_enter(downloadGroup);
    [self getUserInfoWithDownloadGroup:downloadGroup];
    dispatch_group_enter(downloadGroup);
    [self getUserVideoWithDownloadGroup:downloadGroup];
    XMWeakSelf;
    dispatch_group_notify(downloadGroup, dispatch_get_main_queue(), ^{
//        [weakSelf.detailView loadSuccess];
    });
//    [self getUserInfo];
//    [self getUserVideo];
}

- (void)getUserInfoWithDownloadGroup:(dispatch_group_t)downloadGroup {
    XMWeakSelf;
    [_Network getOtherUserInfoWithUserId:_userId
                                 success:^(id  _Nonnull responseObject) {
        XMUserInfoModel *userInfoModel = [XMUserInfoModel yy_modelWithJSON:responseObject[@"data"]];
        weakSelf.attentionStatus = userInfoModel.follow;
        [weakSelf.homePageView setContentWithUserInfoModel:userInfoModel
                                                    userId:weakSelf.userId];
        if (downloadGroup) {
            dispatch_group_leave(downloadGroup);
        }
    } failure:^(NSError * _Nonnull error) {
        if (downloadGroup) {
            dispatch_group_leave(downloadGroup);
        }
    }];
}

- (void)getUserVideoWithDownloadGroup:(dispatch_group_t)downloadGroup {
    XMWeakSelf;
    [_Network getOtherVideoWithUserId:_userId success:^(id  _Nonnull responseObject) {
        NSArray *productArr = [NSArray yy_modelArrayWithClass:[XMPicwordModel class] json:[responseObject safeArrayForKey:@"data"]];
        [weakSelf.homePageView setContentWithProductModelArr:productArr];
        if (downloadGroup) {
            dispatch_group_leave(downloadGroup);
        }
    } failure:^(NSError * _Nonnull error) {
        if (downloadGroup) {
            dispatch_group_leave(downloadGroup);
        }
    }];
}

- (void)attention {
    XMWeakSelf;
    [_Network friendAttentionWithUserId:_userId
                                success:^(id  _Nonnull responseObject) {
        [weakSelf getUserInfoWithDownloadGroup:nil];
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

- (void)cancelAttention {
    XMWeakSelf;
    [_Network cancelAttentionWithUserId:_userId success:^(id  _Nonnull responseObject) {
        [weakSelf getUserInfoWithDownloadGroup:nil];
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

#pragma mark - Delegate
- (void)chatAction {
    
}

- (void)attentionAction {
    if ([_attentionStatus boolValue]) {
        [self cancelAttention];
    }
    else {
        [self attention];
    }
}

- (void)checkMeetListAction {
    XMMyMeetingViewController *myMeetingVC = [[XMMyMeetingViewController alloc] init];
    myMeetingVC.userId = _userId;
    [self.navigationController pushViewController:myMeetingVC animated:YES];
}

- (XMUserHomePageView *)homePageView {
    if (!_homePageView) {
        _homePageView = [[XMUserHomePageView alloc] initWithFrame:self.view.bounds];
        _homePageView.delegate = self;
    }
    return _homePageView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
